package com.demo.designmode.template.case2;

import com.demo.designmode.template.case1.ConnectFactory;
import com.demo.designmode.template.case1.User;
import com.demo.designmode.template.case1.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @author jiaxXM
 * @date 2024/9/3
 */
@Service
public class UserServiceImpl implements UserService {

    //引入模板
    @Resource
    private DaoTemplate daoTemplate;

    private static final String GET_USER_BY_NAME_SQL = "SELECT `id`,`name`,`age`,`job` FROM `tb_user` WHERE `name` = '%s'";

    @Override
    public User getUserByName(String name) throws Exception {
       return daoTemplate.query(String.format(GET_USER_BY_NAME_SQL,name),User.class);
    }

    private static final String GET_USER_BY_ID_SQL = "SELECT `id`,`name`,`age`,`job` FROM `tb_user` WHERE `id` = '%s'";
    @Override
    public User getUserById(Integer id) throws Exception {
        return daoTemplate.query(String.format(GET_USER_BY_ID_SQL,id),User.class);
    }
}
